const express=require('express');
const pool=require('../pool.js')
const router=express.Router();
//注册
router.post('/regsiter',(req,res,next)=>{
  // console.log(req.body);
  pool.query('select * from user where uphone=?',req.body.uphone,(err,result)=>{
    if(err){
      next(err)
      return
    }else{
      // console.log(result);
      if(result.length){
        res.send({code:201,msg:'手机号已存在'})
      }else{
        pool.query('insert into user set ?',req.body,(err,resu)=>{
          if(err){
            next(err)
            return
          }else{
            //affectedRows
            if(resu.affectedRows){
              res.send({code:200,msg:'注册成功'})
            }else{
              res.send({code:202,msg:'注册失败'})
            }
          }
        })
      }
    }
  })
})
//登录
router.post('/login',(req,res,next)=>{
  // console.log(req.body);
  // console.log(req.body.uphone);
  pool.query('select * from user where uphone=? && upwd=?',[req.body.uphone,req.body.upwd],(err,result)=>{
    if(err){
      next(err)
      return
    }else{
      // console.log(result);
      if(result.length){
        res.send({code:200,msg:'登录成功',data:result[0].uid})
      }else{
        res.send({code:201,msg:'登录失败，账号名或密码错误'})
      }
    }
  })
})


module.exports=router;